package pm.devices.drives;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by fat3kb
 * Date: 02.04.2008
 * Time: 22:52:07
 */
public class DrivesChecker implements Runnable {
	private List<DrivesListener> listeners;
	private String[] drivesIds;
	private int mills;

	public DrivesChecker(final int mills) {
		listeners = new ArrayList<DrivesListener>();
		drivesIds = DrivesUtil.getLogicalDrives();
		this.mills = mills;
	}

	public void addListener(final DrivesListener drivesListener) {
		listeners.add(drivesListener);
	}

	public void run() {
		boolean interrupted = false;

		while (!interrupted) {
			final String[] newDrivesIds = DrivesUtil.getLogicalDrives();
			if (!Arrays.deepEquals(drivesIds, newDrivesIds)) {
				drivesIds = newDrivesIds;
				for (final DrivesListener listener : listeners) {
					listener.drivesChanged(DrivesUtil.getDrives(drivesIds));
				}
			}
			try {
				Thread.sleep(mills);
			} catch (InterruptedException e) {
				interrupted = true;
			}
		}
	}
}
